$(function () {
    obj.init();
});

var obj = new function () {
    return {
        dataGrid: null,
        dom: {
            search: null
        },
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'res/getReferenceRepeal');
            this.dataGrid.parseToolbar().init();
            this.dom.search = $('#bt_list_search');
            $('#bt_list_check').menubutton({
                menu: '#menu_referenceRepealCheck'
            });
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            $this.dom.search.click(function () {
                $this.funObj().search();
            });
            xhTool.bindDocKeyDown($this.dom.search);
            //获取区域数据
            $('#school_area').combotree({
                url: 'getDepData',
                animate:true,
                onLoadSuccess:function(node,data){
                    $length = data.length;
                    $.each(data,function (id,value) {
                        if (value.children !== undefined) {
                            $length = $length + value.children.length;
                        }
                    })
                },
                onShowPanel:function(){
                    if ($length <= 8){
                        $(this).combobox('panel').height("auto");//高度自适应
                    }else{
                        $(this).combobox('panel').height("200");//高度自定义
                    }
                },
                onBeforeSelect: function (node) {
                    var t = $(this).tree;
                    var isLeaf = t('isLeaf', node.target);
                    if (!isLeaf) {//选择的不是叶子节点
                        return false;
                    }
                }
            });
            //双击
            this.dataGrid.onDblClickRow = function(rowIndex, rowData){
                var id = rowData['iId'];
                var status = rowData['iStatusName'];
                var createId = rowData['iCreateUserId'];
                var button = '';
                var check = $('#check_data').val();
                var edit = $('#edit_data').val();
                var userId = $('#userId_data').val();
                if (id.length <= 0) {
                    window.top.xhTop.showError('未选中记录！');
                    return false;
                }

                if (edit == 2 && check == 2){
                    button = '保存';
                } else if (edit == 2 && check == 1){
                    if ((status === '待审核') && createId == userId){
                        button = '保存';
                    }
                }
                window.top.xhTop.showModal({
                    title: '申请信息',
                    content: xhTool.getFrameString('res/modifyReferenceRepeal', {id:id}, 250),
                    button: button,
                    size: 'md',
                    width:'800'
                });
            };
            //查看审核详情
            $('.xh-datagrid').on('click','.checkResult',function () {
                var checkResult=$(this).attr('checkResult');
                window.top.xhTop.showMessage({
                    title: '[审核详情]',
                    text: checkResult
                });
            });
        },
        funObj: function () {
            var $this = this;
            return {
                //查找
                search: function () {
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague')),
                            status: xhExtra.getValue($('#list_search_status')),
                            app: xhExtra.getValue($('#list_search_app')),
                            school_area: $('#school_area').combotree('getValue'),
                            iId: xhExtra.getValue($('#list_iId'))
                        }
                    })
                },
                //审核
                setReferRepealCheck: function (level) {
                    var id = $this.dataGrid.getSelectValues('iId');
                    var iStatus = $this.dataGrid.getSelectValue('iStatusName');
                    if (id.length < 1) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    if (id.length > 1){
                        window.top.xhTop.showError('请单选后操作！');
                        return false;
                    }
                    if (iStatus !== '待审核'){
                        window.top.xhTop.showError('该状态已审核，请重新选择');
                        return false;
                    }
                    if(level === 1){//通过
                        window.top.swal({
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '填写说明',
                            html: '<div><textarea type="text" id="record" class="swal2-textarea" placeholder="必填"></textarea></div>',
                            preConfirm: function() {
                                return new Promise(function (resolve, reject) {
                                    var record     = $('#record', window.top.document).val();
                                    xhExtra.ajaxRequest('res/referRepPass', {
                                        id: id,
                                        record: record
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid.reload();
                                })
                            }
                        })
                    } else if(level === 2){//不通过
                        id = $this.dataGrid.getSelectValue('iId');
                        window.top.swal({
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '填写原因',
                            html: '<div><textarea type="text" id="recommend" class="swal2-textarea" placeholder="必填"></textarea></div>',
                            preConfirm: function() {
                                return new Promise(function (resolve, reject) {
                                    var recommend     = $('#recommend', window.top.document).val();
                                    xhExtra.ajaxRequest('res/referRepRefuse', {
                                        id: id,
                                        recommend: recommend
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid.reload();
                                })
                            }
                        })
                    }
                }
            }
        }
    }
};

